package com.xhwl.logistics.service.impl.redis;

import com.xhwl.logistics.dto.push.CarInKeTopDTO;
import com.xhwl.logistics.dto.push.CarOutKeTopDTO;
import com.xhwl.logistics.dto.push.CarPayKeTopDTO;
import com.xhwl.logistics.util.AboutDateUtil;
import org.springframework.data.redis.core.ZSetOperations;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Set;

//@Service
public class VehicleDataPushKeTopRedisServiceImpl {

    @Resource(name = "redisTemplateForObject")
    private ZSetOperations<String, CarInKeTopDTO> zSetOperationsForCarInKeTopDTO;

    @Resource(name = "redisTemplateForObject")
    private ZSetOperations<String, CarOutKeTopDTO> zSetOperationsForCarOutKeTopDTO;

    @Resource(name = "redisTemplateForObject")
    private ZSetOperations<String, CarPayKeTopDTO> zSetOperationsForCarPayKeTopDTO;

    private double score(Date date) {
        return Double.parseDouble(AboutDateUtil.dateToString(date, "yyyyMMddHHmmss"));
    }

    private String key(Integer parkId, Date date, boolean isIn) {
        String vehicleAccessRecordPrefix;
        if (isIn) {
            vehicleAccessRecordPrefix = "cent-logistics:access-record:in:";
        } else {
            vehicleAccessRecordPrefix = "cent-logistics:access-record:out:";
        }
        return vehicleAccessRecordPrefix + parkId + ":" + AboutDateUtil.dateToString(AboutDateUtil.theSameDayOfDate(date), "yyyy-MM-dd");
    }
    private String keyForPay(Integer parkId, Date date) {
        return "cent-logistics:pay-record:" + parkId + ":" + AboutDateUtil.dateToString(AboutDateUtil.theSameDayOfDate(date), "yyyy-MM-dd");
    }

    public void insertAccessRecordForIn(CarInKeTopDTO carInKeTopDTO) {
        Integer parkId = carInKeTopDTO.getParkId();
        Date dateForEntryTime = AboutDateUtil.stringToDate(carInKeTopDTO.getEntryTime());

        String redisKey = key(parkId, dateForEntryTime, true);
        zSetOperationsForCarInKeTopDTO.add(redisKey, carInKeTopDTO, score(dateForEntryTime));
    }
    public void insertAccessRecordForOut(CarOutKeTopDTO carOutKeTopDTO) {
        Integer parkId = carOutKeTopDTO.getParkId();
        Date dateForExitTime = AboutDateUtil.stringToDate(carOutKeTopDTO.getLeaveTime());

        String redisKey = key(parkId, dateForExitTime, false);
        zSetOperationsForCarOutKeTopDTO.add(redisKey, carOutKeTopDTO, score(dateForExitTime));
    }
    public void insertPayRecord(CarPayKeTopDTO carPayKeTopDTO) {
        Integer parkId = carPayKeTopDTO.getParkId();
        Date dateForPayTime = AboutDateUtil.stringToDate(carPayKeTopDTO.getPayTime());

        String redisKey = keyForPay(parkId, dateForPayTime);
        zSetOperationsForCarPayKeTopDTO.add(redisKey, carPayKeTopDTO, score(dateForPayTime));
    }

    public Set<CarInKeTopDTO> queryAccessRecordForIn(Integer parkId, Date date, Date startTime, Date endTime) {
        String redisKey = key(parkId, date, true);
        return zSetOperationsForCarInKeTopDTO.rangeByScore(redisKey, score(startTime), score(endTime));
    }

}
